<template>
	<app-layout :haveBackground="haveBackground">
		
		<!-- #ifdef MP-WEIXIN -->
		<app-my-app></app-my-app>
		<!-- #endif -->
		<view v-if="type === 'mall'">
			<app-index v-if="destroy" :page-hide="pageHide" :home-pages="homePages" :scrollTop="scrollTop" :is_storage="is_storage"></app-index>
		</view>

		<view v-else-if="type === 'diy'">
			<app-diy-page v-if="destroy" :page-hide="pageHide" :home-pages="homePages" :scrollTop="scrollTop" :is_storage="is_storage"></app-diy-page>
		</view>

		<app-buy-prompt v-if="config.is_purchase_frame === `1`"></app-buy-prompt>
	</app-layout>
</template>

<script>
	import appIndex from '../../components/page-component/index/app-index.vue';
	import appDiyPage from '../../components/page-component/index/app-diy-page.vue';
	import appBuyPrompt from '../../components/page-component/app-buy-prompt/app-buy-prompt.vue';
	import {
		getStorageSync,
		setStorageSync
	} from '../../core/cache.js';
	import {
		mapGetters,
		mapState
	} from 'vuex';
	import appMyApp from '../../components/page-component/app-my-app/app-my-app.vue';

	let page_id = 0;

	export default {
		name: 'index',
		components: {
			'app-index': appIndex,
			'app-diy-page': appDiyPage,
			appBuyPrompt,
			appMyApp
		},
		data() {
			return {
				scrollTop: 0,
				homePages: [],
				type: '',
				is_storage: false,
				haveBackground: true,
				// newHomePages: [],
				destroy: true,
				// page_id: 152,
				pageHide: false,
			}
		},
		onShow(e) {
			console.log('page onShow--->'); // 公众号文章进小程序无底部导航调试，请勿删除
			console.log(e); // 公众号文章进小程序无底部导航调试，请勿删除
			console.log('<---page onShow'); // 公众号文章进小程序无底部导航调试，请勿删除
			uni.hideNavigationBarLoading();
			// #ifdef MP-TOUTIAO
			this.destroy = true;
			// #endif
			this.pageHide = false;
		},
		onHide() {
			// #ifdef MP-TOUTIAO
			this.destroy = false;
			// #endif
			this.pageHide = true;
		},
		onLoad(options) {
			console.log('page onLoad--->'); // 公众号文章进小程序无底部导航调试，请勿删除
			console.log(options); // 公众号文章进小程序无底部导航调试，请勿删除
			console.log('<---page onLoad'); // 公众号文章进小程序无底部导航调试，请勿删除
			if (typeof options.scene !== 'undefined') {
				if (options.scene === 'share') {
					this.share(options);
				} else {
					this.qrcode(options);
				}
			} else {
				this.$showLoading();
			}

			// #ifdef MP-ALIPAY
			let query = this.$store.state.page.query;
			if (query && typeof query.scene !== 'undefined') this.qrcode(query);
			// #endif


			if (typeof options.user_id !== 'undefined') {
				this.$store.dispatch('user/setTempParentId', options.user_id)
			}

			page_id = typeof options.page_id !== 'undefined' ? options.page_id : 0;
			// let index_data = getStorageSync('__index_data');
			//
			// if (index_data && page_id == 0) {
			//     this.is_storage = true;
			//     this.type = index_data.type;
			//     this.homePages = index_data.home_pages;
			// }
			this.loadData();
		},
		/* #ifdef MP_WEIXIN || MP_BAIDU */
		onPageScroll(e) {
			this.$store.dispatch('page/actionSetScrollTop', e.scrollTop);
		},
		/* #endif */
		methods: {
			loadData() {
				let that = this;
				//uni.showNavigationBarLoading();
				// uni.showLoading({
				// 	title:'加载中'
				// })
				that.$request({
					url: that.$api.index.index,
					method: 'get',
					data: {
						page_id: page_id,
						longitude: '',
						latitude: '',
					}
				}).then(response => {
					// uni.hideNavigationBarLoading();
					this.$hideLoading();
					//uni.hideLoading()
					let {
						code,
						data
					} = response;
					if (code === 0) {
						that.is_storage = false;
						that.$popupAd.show = null;
						that.homePages = data.home_pages;
						if (that.homePages.navs && that.homePages.navs.length > 0) {
							that.homePages.navs.forEach(function(row, inedx) {
								row.template.data.forEach(function(v, idx) {
									if (v.id == 'background') {
										that.haveBackground = false;
										return false;
									}
								})
							})
						}
						that.type = data.type;
						// if (page_id == 0) setStorageSync('__index_data', data);
						
						var obj = {
							title: '美试客',
							desc:this.mall.setting.share_title ? this.mall.setting.share_title : this.mall.name,
							imgurl: this.mall.setting.share_pic || this.mall.setting.mall_logo_pic,
							mch_id: uni.getStorageSync('s_mch_id') || '0',
							url:''
						}
						if (this.$wx && this.$wx.isWechat()) { //检查是否是微信环境
							this.$wx.shareWX(obj)
						}
					} else {}
				})
			},
			share(options) {
				if (typeof options.params !== 'undefined') {
					let params = JSON.parse(options.params);
					this.$jump({
						url: params.path + '?' + this.$utils.objectToUrlParams(params),
						open_type: 'navigate'
					});
				}
			},
			qrcode(options) {
				this.$request({
					url: this.$api.default.qrcode_parameter,
					data: {
						token: options.scene
					}
				}).then(response => {
					if (response.code === 0) {
						this.$store.dispatch('page/actionSetQeury', null);
						let {
							data,
							path
						} = response.data.detail;
						let url = `/${path}`;
						if (data) {
							url += '?' + this.$utils.objectToUrlParams(data);
							if (typeof data.user_id !== 'undefined') {
								this.$store.dispatch('user/setTempParentId', data.user_id)
							}
						}
						if (`/${path}` != '/pages/index/index') {
							this.$jump({
								url: url,
								open_type: 'navigate'
							});
						}
					}
				}).catch(() => {});
			}
		},
		onShareAppMessage() {
			let args = {
				path: '/pages/index/index',
				params: {}
			};
			if (page_id == 0) {
				args.title = this.mall.setting.share_title ? this.mall.setting.share_title : this.mall.name;
				args.imageUrl = this.mall.setting.share_pic
			} else {
				args.title = this.homePages.title;
				args.params.page_id = page_id;
			}
			return this.$shareAppMessage(args);
		},
		computed: {
			...mapGetters('mallConfig', {
				tabBarNavs: 'getNavBar',
			}),
			...mapState('mallConfig', {
				config: state => state.mall.setting,
				mall: state => state.mall,
			})
		},
	};
</script>
